package com.deyuanyun.pic.pbf.service.impl;

import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.pbf.domain.*;
import com.deyuanyun.pic.pbf.enumeration.ImgCustomType;
import com.deyuanyun.pic.pbf.enumeration.RepairStateEnum;
import com.deyuanyun.pic.pbf.mapper.*;
import com.deyuanyun.pic.pbf.service.*;
import com.deyuanyun.pic.settings.domain.base.Attachment;
import com.deyuanyun.pic.settings.domain.util.DomainInitUtil;
import com.deyuanyun.pic.settings.mapper.base.AttachmentMapper;
import com.deyuanyun.pic.settings.service.base.AttachmentService;
import com.deyuanyun.pic.settings.support.utils.BaseCodeUtils;
import com.deyuanyun.pic.wf.domain.ProcessDefinition;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 非内检测业务实现层
 * @author lijun
 * @date 2016-06-25
 * 
 * History: [修改人][时间][修改描述]
 */
@Service
public class ExternalRepairServiceImpl implements ExternalRepairService{

    @Resource   //防腐层修复
    private CoatRepairMapper coatRepairMapper;
    @Resource   //A，B型套筒补强修复
    private RepairABTypeSleeveMapper repairABTypeSleeveMapper;
    @Resource   //复合材料补强修复表
    private RepairCompositesMapper repairCompositesMapper;
    @Resource   //钢质环氧套筒补强修复表
    private RepairEpoxySleeveMapper repairEpoxySleeveMapper;
    @Resource   //换管
    private RepairReplacePipeMapper repairReplacePipeMapper;
    @Resource   //换管焊接
    private RepairPipeWeldingMapper repairPipeWeldingMapper;
    @Resource   //换管焊缝
    private RepairPipeWeldMapper repairPipeWeldMapper;
    @Resource   //其他修复方法
    private RepairOtherMapper repairOtherMapper;
    @Resource   //附件表
    private AttachmentService attachmentService;
    @Resource   //非内检缺陷
    private PipeBodyDefectMapper pipeBodyDefectMapper;
    @Resource
    private ExternalRepairMapper externalRepairMapper;


    //新增维修记录
    public int repairCoatCreate(CoatRepair coat ,String defectId,String[] att){
        int num = 0;
        if(coat != null) {
            //创建id、时间、用户
            DomainInitUtil.initCreateDomain(coat);
            //逻辑删除字段
            coat.setDeleted((short)0);
            num = coatRepairMapper.insertSelective(coat);
            if (num > 0) {
                //增加缺陷与维修记录对应信息 以及 附件信息
                createRcordAndAtt(defectId,coat.getId(),att);
            }
        }
        return num;
    }

    @Override
    public int repairCompositesCreate(RepairComposites composites,String defectId,String[] att) {
        int num = 0;
        if(composites != null) {
            //创建id、时间、用户
            DomainInitUtil.initCreateDomain(composites);
            //逻辑删除字段
            composites.setDeleted((short)0);
            num = repairCompositesMapper.insertSelective(composites);
            if (num > 0) {
                //增加缺陷与维修记录对应信息 以及 附件信息
                createRcordAndAtt(defectId,composites.getId(),att);
            }
        }
        return num;
    }

    @Override
    public int repairEpoxySleeveCreate(RepairEpoxySleeve epoxySleeve,String defectId,String[] att) {
        int num = 0;
        if(epoxySleeve != null) {
            //创建id、时间、用户
            DomainInitUtil.initCreateDomain(epoxySleeve);
            //逻辑删除字段
            epoxySleeve.setDeleted((short)0);
            num = repairEpoxySleeveMapper.insertSelective(epoxySleeve);
            if (num > 0) {
                //增加缺陷与维修记录对应信息 以及 附件信息
                createRcordAndAtt(defectId,epoxySleeve.getId(),att);
            }
        }
        return num;
    }

    @Override
    public int repairABTypeSleeveCreate(RepairABTypeSleeve ABTypeSleeve,String defectId,String[] att) {
        int num = 0;
        if(ABTypeSleeve != null) {
            //创建id、时间、用户
            DomainInitUtil.initCreateDomain(ABTypeSleeve);
            //逻辑删除字段
            ABTypeSleeve.setDeleted((short)0);
            num = repairABTypeSleeveMapper.insertSelective(ABTypeSleeve);
            if (num > 0) {
                //增加缺陷与维修记录对应信息 以及 附件信息
                createRcordAndAtt(defectId,ABTypeSleeve.getId(),att);
            }
        }
        return num;
    }

    @Override
    public int repairReplacePipeCreate(RepairReplacePipe replacePipe, String defectId, String[] imgsReplace,
                                                      RepairPipeWelding repairPipeWelding, String imgsWelding[],
                                                      List<RepairPipeWeld> repairPipeWeldList) {
        int num = 0;
        int successNum=0;
        if(replacePipe != null) {
            //创建id、时间、用户
            DomainInitUtil.initCreateDomain(replacePipe);
            //逻辑删除字段
            replacePipe.setDeleted((short)0);
            num = repairReplacePipeMapper.insertSelective(replacePipe);
            if (num > 0) {
                //增加缺陷与维修记录对应信息 以及 附件信息
                createRcordAndAtt(defectId,replacePipe.getId(),imgsReplace);
                //添加换管焊接记录
                repairPipeWelding.setReplacePipeId(replacePipe.getId());
                //创建id、时间、用户
                DomainInitUtil.initCreateDomain(repairPipeWelding);
                //逻辑删除字段
                repairPipeWelding.setDeleted((short)0);
                num = repairPipeWeldingMapper.insertSelective(repairPipeWelding);
                //添加换管焊接附件
                if (num > 0 && imgsWelding != null && imgsWelding.length > 0 ) {
                    //增加图片附件
                    for (int i = 0; i < imgsWelding.length; i++) {
                        attachmentService.updateBusinessIdById(repairPipeWelding.getId(), imgsWelding[i]);
                    }
                }
                //添加换管焊缝记录

                for(RepairPipeWeld weld: repairPipeWeldList){
                    weld.setRepairPipeWeldingId(repairPipeWelding.getId());
                    //创建id、时间、用户
                    DomainInitUtil.initCreateDomain(weld);
                    successNum += repairPipeWeldMapper.insertSelective(weld);
                }

            }
        }
        return successNum;
    }

    @Override
    public String repairPipeWeldingCreate(RepairPipeWelding repairPipeWelding) {
        String repairPipeWeldingId = null;
        //创建id、时间、用户
        DomainInitUtil.initCreateDomain(repairPipeWelding);
        //逻辑删除字段
        repairPipeWelding.setDeleted((short)0);
        int i = repairPipeWeldingMapper.insertSelective(repairPipeWelding);
        if (i > 0) {
            repairPipeWeldingId = repairPipeWelding.getId();
        }
        return repairPipeWeldingId;
    }

    @Override
    public int repairPipeWeldCreate(RepairPipeWeld repairPipeWeld) {
        //创建id、时间、用户
        DomainInitUtil.initCreateDomain(repairPipeWeld);
        //逻辑删除字段
        repairPipeWeld.setDeleted((short)0);
        int i = repairPipeWeldMapper.insertSelective(repairPipeWeld);

        return i;
    }

    @Override
    public int repairOtherCreate(RepairOther other,String defectId,String[] att) {
        int num = 0;
        if(other != null) {
            //创建id、时间、用户
            DomainInitUtil.initCreateDomain(other);
            //逻辑删除字段
            other.setDeleted((short)0);
            num = repairOtherMapper.insertSelective(other);
            if (num > 0) {
                //增加缺陷与维修记录对应信息 以及 附件信息
                createRcordAndAtt(defectId,other.getId(),att);
            }
        }
        return num;
    }


    @Override
    public CoatRepair queryRepairCoatByDefectId(String defectId) {
        CoatRepair  coatRepair = externalRepairMapper.queryRepairCoatByDefectId(defectId);
        return coatRepair;
    }

    @Override
    public RepairComposites queryRepairCompositesByDefectId(String defectId) {
        RepairComposites repairComposites = externalRepairMapper.queryRepairCompositesByDefectId(defectId);
        return repairComposites;
    }

    @Override
    public RepairEpoxySleeve queryRepairEpoxySleeveByDefectId(String defectId) {
        RepairEpoxySleeve repairEpoxySleeve = externalRepairMapper.queryRepairEpoxySleeveByDefectId(defectId);
        return repairEpoxySleeve;
    }

    @Override
    public RepairABTypeSleeve queryRepairABTypeSleeveByDefectId(String defectId) {
        RepairABTypeSleeve repairABTypeSleeve = externalRepairMapper.queryRepairABTypeSleeveByDefectId(defectId);
        return repairABTypeSleeve;
    }

    @Override
    public RepairReplacePipe queryRepairReplacePipeByDefectId(String defectId) {
        RepairReplacePipe repairReplacePipe = externalRepairMapper.queryRepairReplacePipeByDefectId(defectId);
        return repairReplacePipe;
    }

    @Override
    public RepairPipeWelding queryRepairPipeWeldingByReplacePipeId(String replacePipeId) {
        RepairPipeWelding repairPipeWelding = externalRepairMapper.queryRepairPipeWeldingByReplacePipeId(replacePipeId);
        return repairPipeWelding;
    }

    @Override
    public List<RepairPipeWeld> queryRepairPipeWeldByWeldingId(String weldingId) {
        List<RepairPipeWeld> repairPipeWeld = externalRepairMapper.queryRepairPipeWeldByWeldingId(weldingId);
        return repairPipeWeld;
    }

    @Override
    public RepairOtherVO queryRepairOtherByDefectId(String defectId) {
        RepairOtherVO repairOther = externalRepairMapper.queryRepairOtherByDefectId(defectId);
        return repairOther;
    }

    @Override
    public int repairRcordAndDefectCreate(String defectId, String repairId) {
        repairRecordDefectVO rd = new repairRecordDefectVO();
        rd.setDefectId(defectId);
        rd.setRepairId(repairId);
        //创建id、时间、用户
        DomainInitUtil.initCreateDomain(rd);
        return externalRepairMapper.repairRcordAndDefectCreate(rd);
    }

    @Override
    public int createRcordAndAtt(String defectId, String repairId, String[] att) {
        if (att != null && att.length > 0 ) {
            //增加图片附件
            for (int i = 0; i < att.length; i++) {
                attachmentService.updateBusinessIdById(repairId, att[i]);
            }
        }
        //增加缺陷和维修记录对应信息
        repairRecordDefectVO rd = new repairRecordDefectVO();
        rd.setDefectId(defectId);
        rd.setRepairId(repairId);
        //创建id、时间、用户
        DomainInitUtil.initCreateDomain(rd);
        int num = externalRepairMapper.repairRcordAndDefectCreate(rd);
        int i = 0;
        //创建缺陷与维修记录成功，更改缺陷维修状态为已维修
        if(num>0){
            PipeBodyDefect defect = new PipeBodyDefect();
            defect.setId(defectId);
            defect.setRepairState("ALREADY_REPAIR");
            i = pipeBodyDefectMapper.updateByPrimaryKeySelective(defect);
        }

        return i;
    }

    @Override
    public ProcessDefinition queryRepairNameByDefectId(String defectId) {
        return externalRepairMapper.queryRepairNameByDefectId(defectId);
    }

    @Override
    public List<Attachment> queryAttachmentOrderByCustom(String businessId){
        List<Attachment> list = new ArrayList<Attachment>();
        //修复前
        list =attachmentService.queryAttachmentByCustom(businessId, ImgCustomType.REPAIR_BEFORE.toString());
        //修复中
        list.addAll(attachmentService.queryAttachmentByCustom(businessId,ImgCustomType.REPAIR_DURING.toString()));
        //修复后
        list.addAll(attachmentService.queryAttachmentByCustom(businessId,ImgCustomType.REPAIR_AFTER.toString()));
        //焊接作业指导书
        list.addAll(attachmentService.queryAttachmentByCustom(businessId,ImgCustomType.WELDWORKTYPE.toString()));
        //无损检测报告
        list.addAll(attachmentService.queryAttachmentByCustom(businessId,ImgCustomType.NDTTYPE.toString()));
        //all
        list.addAll(attachmentService.queryAttachmentByCustom(businessId,ImgCustomType.ALL.toString()));
        return list;
    }
}
